package com.zhiqianbao.adapter;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zhiqianbao.R;
import com.zhiqianbao.entity.MessageAndPushInfo;
import com.zhiqianbao.utils.Constants;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import cn.trinea.android.common.util.StringUtils;

/**
 * Created by liang on 2016/2/20.
 */
public class SysMessageListAdapter extends BaseAdapter {

    List<MessageAndPushInfo> list;
    private LayoutInflater inflater;
    private Activity ctx;

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public SysMessageListAdapter(Activity context, List<MessageAndPushInfo> list) {
        this.ctx = context;
        this.list = list;
        this.inflater = LayoutInflater.from(context);
    }

    private final class ListItemView {
        LinearLayout llyMy;
        TextView tvTitle;
        TextView tvMsg;
        TextView tvType;
        TextView tvTime;
        ImageView isNoRead;
    }


    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ListItemView listItemView = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.sys_msg_item_list, null);
            listItemView = new ListItemView();

            listItemView.llyMy = (LinearLayout) convertView.findViewById(R.id.lly_my);

            listItemView.tvTitle = (TextView) convertView.findViewById(R.id.tv_title);
            listItemView.tvMsg = (TextView) convertView.findViewById(R.id.tv_msg);
            listItemView.tvType = (TextView) convertView.findViewById(R.id.tv_type);
            listItemView.tvTime = (TextView) convertView.findViewById(R.id.tv_time);
            listItemView.isNoRead = (ImageView) convertView.findViewById(R.id.is_no_read);

            convertView.setTag(listItemView);
        } else {
            listItemView = (ListItemView) convertView.getTag();
        }

        MessageAndPushInfo messageAndPushInfo = list.get(position);
        String title = messageAndPushInfo.getTitle();
        title = title == null ? "无" : title;

        listItemView.tvTitle.setText("标题：" + title);
        listItemView.tvMsg.setText(messageAndPushInfo.getContent());

        if(messageAndPushInfo.getState().equals("1")){
            listItemView.isNoRead.setVisibility(View.VISIBLE);
        } else {
            listItemView.isNoRead.setVisibility(View.GONE);
        }

        String type = "";
        if (messageAndPushInfo.getType().equals("message")) {
            type = "短信";
        } else {
            type = "推送";
        }
        listItemView.tvType.setText(type);
        listItemView.tvTime.setText(sdf.format(new Date(messageAndPushInfo.getSendTime())));

        final String infoType = messageAndPushInfo.getCategory();

        final ListItemView finalListItemView = listItemView;
        listItemView.llyMy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finalListItemView.isNoRead.setVisibility(View.GONE);
                if (StringUtils.isBlank(infoType)) {
                    return;
                }
                switch (infoType) {
                    /**
                     * 用户审核类型
                     */
                    case Constants.USER_INFO_AUDIT_TYPE :
                       // CheckUtils.checkUserState(ctx, TakeIdCardMakePicActivity.class);
                        break;
                    /**
                     * 催收贷款类型
                     */
                    case Constants.COLLECTION_LOAN_TYPE :
                        //还款详情
//                        Intent intent = new Intent(ctx, BeforeRepayActivity.class);
//                        ctx.startActivity(intent);
                        break;
                    /**
                     * 附件提醒类型，到添加附件
                     */
                    case Constants.ATTACHMENT_REMIND_TYPE :
                        //CheckUtils.checkUserState(ctx, AttachmentListActivity.class);
                        break;
                    /**
                     * 放款成功提醒
                     */
                    case Constants.LOAN_SUCCESS_TYPE :
                        //进度
//                        Intent intent1 = new Intent(ctx, ProgessApplySingleActivity.class);
//                        ctx.startActivity(intent1);
                        break;
                    /**
                     * 借款审核结论提醒
                     */
                    case Constants.LOAN_APPLY_AUDIT_TYPE :
                        //进度
//                        Intent intent2 = new Intent(ctx, ProgessApplySingleActivity.class);
//                        ctx.startActivity(intent2);
                        break;
                    /**
                     * 通知用户换卡
                     */
                    case Constants.NOTICE_USER_CHANGE_CARD_TYPE :
//                        CheckUtils.checkUserState(ctx, BankCardInfoActivity.class);
                        break;
                    /**
                     * 取消用户放款
                     */
                    case Constants.CANCEL_USER_LOAN_TYPE :
                        //进度
//                        Intent intent3 = new Intent(ctx, ProgessApplySingleActivity.class);
//                        ctx.startActivity(intent3);
                        break;
                }
            }
        });

        return convertView;
    }
}
